package ar.com.e2solver.solver.operator.impl;

import ar.com.e2solver.model.Puzzle;
import ar.com.e2solver.solver.Individual;
import ar.com.e2solver.solver.RandomConstants;
import ar.com.e2solver.solver.operator.MutationOperator;

public final class MultipleRotateMutationOperator implements MutationOperator {
	
	private static MultipleRotateMutationOperator instance = new MultipleRotateMutationOperator();
	
	private MultipleRotateMutationOperator(){}

	public static MultipleRotateMutationOperator getInstance() {
		return instance;
	}

	public Individual operate(Individual individual) {
		// Crea una copia
		Puzzle puzzle = new Puzzle(individual.getPuzzle());
		int cantRotations = (puzzle.getColumnNumbers() * puzzle.getRowsNumber())/10;
		
		// La mutacion es solo con las piezas internas (ni bordes ni esquinas)
		int xRandom, yRandom;
		if(puzzle.getRowsNumber() > 2 && puzzle.getColumnNumbers() > 2){
			for(int i = 0; i < cantRotations; i++){
				xRandom = RandomConstants.RANDOM.nextInt(puzzle.getRowsNumber() - 2) + 1;
				yRandom = RandomConstants.RANDOM.nextInt(puzzle.getColumnNumbers() - 2) + 1;
				puzzle.getPieceAt(xRandom, yRandom).rotateToRight(1);	
			}
			
		}
		
		return new Individual(puzzle);
	}

}
